<template><div><h2 id="配置-configurations" tabindex="-1"><a class="header-anchor" href="#配置-configurations"><span>配置（Configurations）</span></a></h2>
<p>在 Yii 中，创建新对象和初始化已存在对象时广泛使用配置。配置通常包含被创建对象的类名和一组将要赋值给对象 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-properties.md" target="_blank" rel="noopener noreferrer">属性</a> 的初始值。还可能包含一组将被附加到对象 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-events.md" target="_blank" rel="noopener noreferrer">事件</a> 上的句柄。和一组将被附加到对象上的 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-behaviors.md" target="_blank" rel="noopener noreferrer">行为</a>。</p>
<p>以下代码中的配置被用来创建并初始化一个数据库连接：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$config</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\db\Connection'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'dsn'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'mysql:host=127.0.0.1;dbname=demo'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'root'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">''</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'charset'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'utf8'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$db</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token function">createObject</span><span class="token punctuation">(</span><span class="token variable">$config</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>[[Yii::createObject()]] 方法接受一个配置数组并根据数组中指定的类名创建对象。对象实例化后，剩余的参数被用来初始化对象的属性，事件处理和行为。</p>
<p>对于已存在的对象，可以使用 [[Yii::configure()]] 方法根据配置去初始化其属性，就像这样：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token function">configure</span><span class="token punctuation">(</span><span class="token variable">$object</span><span class="token punctuation">,</span> <span class="token variable">$config</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>请注意，如果配置一个已存在的对象，那么配置数组中不应该包含指定类名的 <code v-pre>class</code> 元素。</p>
<h2 id="配置格式" tabindex="-1"><a class="header-anchor" href="#配置格式"><span>配置格式</span></a></h2>
<p>一个配置的格式可以描述为以下形式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'ClassName'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'propertyName'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'propertyValue'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'on eventName'</span> <span class="token operator">=></span> <span class="token variable">$eventHandler</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'as behaviorName'</span> <span class="token operator">=></span> <span class="token variable">$behaviorConfig</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>其中</p>
<ul>
<li><code v-pre>class</code> 元素指定了将要创建的对象的完全限定类名。</li>
<li><code v-pre>propertyName</code> 元素指定了对象属性的初始值。键名是属性名，值是该属性对应的初始值。 只有公共成员变量以及通过 getter/setter 定义的 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-properties.md" target="_blank" rel="noopener noreferrer">属性</a> 可以被配置。</li>
<li><code v-pre>on eventName</code> 元素指定了附加到对象 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-events.md" target="_blank" rel="noopener noreferrer">事件</a>上的句柄是什么。请注意，数组的键名由 <code v-pre>on</code> 前缀加事件名组成。请参考 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-events.md" target="_blank" rel="noopener noreferrer">事件</a>章节了解事件句柄格式。</li>
<li><code v-pre>as behaviorName</code> 元素指定了附加到对象的 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-behaviors.md" target="_blank" rel="noopener noreferrer">行为</a>。请注意，数组的键名由 <code v-pre>as</code>前缀加行为名组成。<code v-pre>$behaviorConfig</code> 值表示创建行为的配置信息，格式与我们之前描述的配置格式一样。</li>
</ul>
<p>下面是一个配置了初始化属性值，事件句柄和行为的示例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'app\components\SearchEngine'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'apiKey'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'xxxxxxxx'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'on search'</span> <span class="token operator">=></span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"搜索的关键词： "</span> <span class="token operator">.</span> <span class="token variable">$event</span><span class="token operator">-></span><span class="token property">keyword</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'as indexer'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'app\components\IndexerBehavior'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token comment">// ... 初始化属性值 ...</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="使用配置" tabindex="-1"><a class="header-anchor" href="#使用配置"><span>使用配置</span></a></h2>
<p>Yii 中的配置可以用在很多场景。本章开头我们展示了如何使用 [[Yii::creatObject()]] 根据配置信息创建对象。本小节将介绍配置的两种主要用法：配置应用与配置小部件。</p>
<h3 id="应用配置" tabindex="-1"><a class="header-anchor" href="#应用配置"><span>应用配置</span></a></h3>
<p><a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-applications.md" target="_blank" rel="noopener noreferrer">应用</a> 的配置可能是最复杂的配置之一。因为 <code v-pre>yii\web\Application</code> 类拥有很多可配置的属性和事件。更重要的是它的 [[yii\web\Application::components]] 属性可以接收配置数组并通过应用注册为组件。以下是一个针对 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/start-installation.md" target="_blank" rel="noopener noreferrer">基础应用模板</a> 的应用配置概要：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$config</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'basic'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'basePath'</span> <span class="token operator">=></span> <span class="token function">dirname</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'extensions'</span> <span class="token operator">=></span> <span class="token keyword">require</span> <span class="token constant">__DIR__</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'/../vendor/yiisoft/extensions.php'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'cache'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\caching\FileCache'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'mailer'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\swiftmailer\Mailer'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'log'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\log\Dispatcher'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'traceLevel'</span> <span class="token operator">=></span> <span class="token constant">YII_DEBUG</span> <span class="token operator">?</span> <span class="token number">3</span> <span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'targets'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\log\FileTarget'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'db'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\db\Connection'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'dsn'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'mysql:host=localhost;dbname=stay2'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'root'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">''</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'charset'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'utf8'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>配置中没有 <code v-pre>class</code> 键的原因是这段配置应用在下面的入口脚本中，类名已经指定了。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified">yii<span class="token punctuation">\</span>web<span class="token punctuation">\</span>Application</span><span class="token punctuation">(</span><span class="token variable">$config</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>更多关于应用 <code v-pre>components</code> 属性配置的信息可以查阅 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-applications.md" target="_blank" rel="noopener noreferrer">应用</a> 以及 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-service-locator.md" target="_blank" rel="noopener noreferrer">服务定位器</a> 章节。</p>
<p>自版本 2.0.11 开始，系统配置支持使用 <code v-pre>container</code> 属性来配置 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-di-container.md" target="_blank" rel="noopener noreferrer">依赖注入容器</a> 例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$config</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'basic'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'basePath'</span> <span class="token operator">=></span> <span class="token function">dirname</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'extensions'</span> <span class="token operator">=></span> <span class="token keyword">require</span> <span class="token constant">__DIR__</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'/../vendor/yiisoft/extensions.php'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'container'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'definitions'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'yii\widgets\LinkPager'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'maxButtonCount'</span> <span class="token operator">=></span> <span class="token number">5</span><span class="token punctuation">]</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'singletons'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token comment">// 依赖注入容器单例配置</span></span>
<span class="line">        <span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>请参考 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-di-container.md" target="_blank" rel="noopener noreferrer">依赖注入容器</a> 下面的 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-di-container.md#advanced-practical-usage" target="_blank" rel="noopener noreferrer">高级应用实例</a> 获取更多 <code v-pre>definitions</code> 和 <code v-pre>singletons</code> 配置项和实际使用的例子。</p>
<h3 id="小部件配置" tabindex="-1"><a class="header-anchor" href="#小部件配置"><span>小部件配置</span></a></h3>
<p>使用 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-widgets.md" target="_blank" rel="noopener noreferrer">小部件</a>时，常常需要配置以便自定义其属性。 [[yii\base\Widget::widget()]] 和 [[yii\base\Widget::begin()]] 方法都可以用来创建小部件。它们可以接受配置数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>widgets<span class="token punctuation">\</span>Menu</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">echo</span> <span class="token class-name static-context">Menu</span><span class="token operator">::</span><span class="token function">widget</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'activateItems'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'items'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'label'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Home'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'site/index'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'label'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Products'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'product/index'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'label'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Login'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'site/login'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'visible'</span> <span class="token operator">=></span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">user</span><span class="token operator">-></span><span class="token property">isGuest</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>上述代码创建了一个小部件 <code v-pre>Menu</code> 并将其 <code v-pre>activateItems</code> 属性初始化为 false。<code v-pre>item</code> 属性也配置成了将要显示的菜单条目。</p>
<p>请注意，代码中已经给出了类名 <code v-pre>yii\widgets\Menu</code>，配置数组<strong>不应该</strong>再包含 <code v-pre>class</code> 键。</p>
<h2 id="配置文件" tabindex="-1"><a class="header-anchor" href="#配置文件"><span>配置文件</span></a></h2>
<p>当配置的内容十分复杂，通用做法是将其存储在一或多个 PHP 文件中，这些文件被称为 <em>配置文件</em>。一个配置文件返回的是 PHP 数组。例如，像这样把应用配置信息存储在名为 <code v-pre>web.php</code> 的文件中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'basic'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'basePath'</span> <span class="token operator">=></span> <span class="token function">dirname</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'extensions'</span> <span class="token operator">=></span> <span class="token keyword">require</span> <span class="token constant">__DIR__</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'/../vendor/yiisoft/extensions.php'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token keyword">require</span> <span class="token constant">__DIR__</span> <span class="token operator">.</span> <span class="token string single-quoted-string">'/components.php'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>鉴于 <code v-pre>components</code> 配置也很复杂，上述代码把它们存储在单独的 <code v-pre>components.php</code> 文件中，并且包含在 <code v-pre>web.php</code> 里。<code v-pre>components.php</code> 的内容如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'cache'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\caching\FileCache'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'mailer'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\swiftmailer\Mailer'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'log'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\log\Dispatcher'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'traceLevel'</span> <span class="token operator">=></span> <span class="token constant">YII_DEBUG</span> <span class="token operator">?</span> <span class="token number">3</span> <span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'targets'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\log\FileTarget'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'db'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\db\Connection'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'dsn'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'mysql:host=localhost;dbname=stay2'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'root'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">''</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'charset'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'utf8'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>仅仅需要 “require”，就可以取得一个配置文件的配置内容，像这样：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$config</span> <span class="token operator">=</span> <span class="token keyword">require</span> <span class="token string single-quoted-string">'path/to/web.php'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified">yii<span class="token punctuation">\</span>web<span class="token punctuation">\</span>Application</span><span class="token punctuation">(</span><span class="token variable">$config</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="默认配置" tabindex="-1"><a class="header-anchor" href="#默认配置"><span>默认配置</span></a></h2>
<p>[[Yii::createObject()]] 方法基于<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-di-container.md" target="_blank" rel="noopener noreferrer">依赖注入容器</a>实现。使用 [[Yii::creatObject()]] 创建对象时，可以附加一系列 <strong>默认配置</strong> 到指定类的任何实例。默认配置还可以在 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/runtime-bootstrapping.md" target="_blank" rel="noopener noreferrer">入口脚本</a> 中调用 <code v-pre>Yii::$container-&gt;set()</code> 来定义。</p>
<p>例如，如果你想自定义 [[yii\widgets\LinkPager]] 小部件，以便让分页器最多只显示 5 个翻页按钮（默认是 10 个），你可以用下述代码实现：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Yii</span><span class="token operator">::</span><span class="token variable">$container</span><span class="token operator">-></span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'yii\widgets\LinkPager'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'maxButtonCount'</span> <span class="token operator">=></span> <span class="token number">5</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>不使用默认配置的话，你就得在任何使用分页器的地方，都配置 <code v-pre>maxButtonCount</code> 的值。</p>
<h2 id="环境常量" tabindex="-1"><a class="header-anchor" href="#环境常量"><span>环境常量</span></a></h2>
<p>配置经常要随着应用运行的不同环境更改。例如在开发环境中，你可能使用名为 <code v-pre>mydb_dev</code> 的数据库， 而生产环境则使用 <code v-pre>mydb_prod</code> 数据库。为了便于切换使用环境，Yii 提供了一个定义在入口脚本中的 <code v-pre>YII_ENV</code> 常量。 如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">defined</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'YII_ENV'</span><span class="token punctuation">)</span> <span class="token keyword">or</span> <span class="token function">define</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'YII_ENV'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'dev'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以把 <code v-pre>YII_ENV</code> 定义成以下任何一种值：</p>
<ul>
<li><code v-pre>prod</code>：生产环境。常量 <code v-pre>YII_ENV_PROD</code> 将被看作 true。 如果你没修改过，这就是 <code v-pre>YII_ENV</code> 的默认值。</li>
<li><code v-pre>dev</code>：开发环境。常量 <code v-pre>YII_ENV_DEV</code> 将被看作 true。</li>
<li><code v-pre>test</code>：测试环境。常量 <code v-pre>YII_ENV_TEST</code> 将被看作 true。</li>
</ul>
<p>有了这些环境常量，你就可以根据当下应用运行环境的不同，进行差异化配置。例如，应用可以包含下述代码只在开发环境中开启 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/tool-debugger.md" target="_blank" rel="noopener noreferrer">调试工具</a>。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$config</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token operator">...</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">YII_ENV_DEV</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 根据 `dev` 环境进行的配置调整</span></span>
<span class="line">    <span class="token variable">$config</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'bootstrap'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'debug'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$config</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'modules'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'debug'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'yii\debug\Module'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token variable">$config</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


